package com.pengcheng.taskapplication.mvp.view.activity;

import android.app.Activity;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.view.KeyEvent;
import android.widget.ImageView;


import com.pengcheng.taskapplication.R;
import com.pengcheng.taskapplication.contants.ConstantValue;
import com.pengcheng.taskapplication.mvp.view.fragment.IndexFragment;
import com.pengcheng.taskapplication.mvp.view.fragment.MyInfoFragment;
import com.pengcheng.taskapplication.mvp.view.fragment.NoticeListFragment;
import com.pengcheng.taskapplication.mvp.view.fragment.OrderFragment;
import com.pengcheng.taskapplication.mvp.view.widget.FragementViewPager;
import com.pengcheng.taskapplication.mvp.view.widget.MainViewPagerAdapter;
import com.pengcheng.taskapplication.mvp.view.widget.bottombarlayout.BottomBarItem;
import com.pengcheng.taskapplication.mvp.view.widget.bottombarlayout.BottomBarLayout;
import com.pengcheng.taskapplication.rxbus.RxBus;
import com.pengcheng.taskapplication.rxbus.RxBusRoute;
import com.pengcheng.taskapplication.rxbus.RxEvent;
import com.pengcheng.taskapplication.util.SPUtils;
import com.pengcheng.taskapplication.util.ToastUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends BaseActivity {


    @BindView(R.id.vp_content)
    FragementViewPager viewPager;
    @BindView(R.id.tab1_home)
    BottomBarItem tab1Home;
    @BindView(R.id.tab4_task)
    BottomBarItem tab4Task;
    @BindView(R.id.tab2_tuijian)
    BottomBarItem tab2Tuijian;
    @BindView(R.id.tab3_my)
    BottomBarItem tab3My;
    @BindView(R.id.bbl_main)
    BottomBarLayout bblMain;

    private ArrayList<Fragment> fragmentList = new ArrayList<>();
    private int currentIndex = 0;
    private MainViewPagerAdapter adpter;
    private long TOUCH_TIME = 0;
    private static final long WAIT_TIME = 2000L;

    public static Activity mActivity;
    @Override
    protected int layoutResID() {
        return R.layout.activity_main;
    }


    @Override
    protected void initData() {
        //初始化tab
        mActivity=this;
        initTab();
    }





    private void initTab() {
        IndexFragment indexFragment = new IndexFragment();
        NoticeListFragment noticeListFragment = new NoticeListFragment();
        OrderFragment orderFragment = new OrderFragment();
        MyInfoFragment myInfoFragment = new MyInfoFragment();
        fragmentList.add(indexFragment);
        fragmentList.add(noticeListFragment);
        fragmentList.add(orderFragment);
        fragmentList.add(myInfoFragment);


        adpter = new MainViewPagerAdapter(getSupportFragmentManager(), fragmentList);
        viewPager.setAdapter(adpter);
        viewPager.setOffscreenPageLimit(fragmentList.size());
        bblMain.setViewPager(viewPager);
        bblMain.setCurrentItem(0);
        bblMain.setOnItemSelectedListener((bottomBarItem, position) -> {
            //选择之后的处理
            currentIndex = position;

        });
    }

    public void goRecommend(int flag) {
        if (bblMain != null) {
            bblMain.setCurrentItem(flag);
        }
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN) {
            if (keyCode == KeyEvent.KEYCODE_BACK) {
                if (System.currentTimeMillis() - TOUCH_TIME < WAIT_TIME) {
                    finish();
                } else {
                    TOUCH_TIME = System.currentTimeMillis();
                    ToastUtils.showShortToast("再按一次退出");
                }
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }


    @Override
    protected boolean isRegistRxBus() {
        return true;
    }

    /**
     * 注册订阅事件
     */
    protected void receiveEvent(RxEvent rxEvent) {
        switch (rxEvent.busName) {
            case RxBusRoute.LOGIN_OUT:
//                //退出登录
//                //userid  //nick  //avatar  //recode
//                SPUtils.getInstance().remove(ConstantValue.SpType.userid);
//                SPUtils.getInstance().remove(ConstantValue.SpType.recode);
//                SPUtils.getInstance().remove(ConstantValue.SpType.newavatar);
//                startActivity(new Intent(context, LoginActivity.class));
//                finish();
                break;

            case RxBusRoute.TABORDER:
                //选中公告
                goRecommend(1);
                break;

            case RxBusRoute.Park:
                //选中洗车
                goRecommend(2);
                RxBus.getInstance().post(new RxEvent(RxBusRoute.ParkOrder));
                break;
            case RxBusRoute.Wash:
                //选中洗车
                goRecommend(2);
                RxBus.getInstance().post(new RxEvent(RxBusRoute.WashOrder));
                break;


            case RxBusRoute.gotoindex:
                //回到首页
                goRecommend(0);
                RxBus.getInstance().post(new RxEvent(RxBusRoute.indexfresh));
                break;



        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }


}
